package wiki.xsx.core.snowflake.api;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wiki.xsx.core.snowflake.config.Snowflake;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 雪花 API
 * @author xsx
 * @date 2019/11/20
 * @since 1.8
 */
@RestController
@RequestMapping("/snowflake")
public class SnowflakeApi {

    @Resource(type = Snowflake.class)
    private Snowflake snowflake;

    @GetMapping("/long")
    public Long getLong() {
        return this.snowflake.nextId();
    }

    @GetMapping("/longs")
    public List<Long> getLongs(int count) {
        List<Long> list = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            list.add(this.snowflake.nextId());
        }
        return list;
    }

    @GetMapping("/string")
    public String getString() {
        return this.snowflake.nextIdStr();
    }

    @GetMapping("/strings")
    public List<String> getStrings(int count) {
        List<String> list = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            list.add(this.snowflake.nextIdStr());
        }
        return list;
    }


    @GetMapping("/timestamp")
    public Long getTimestamp(Long id) {
        return this.snowflake.getTimestamp(id);
    }

    @PostMapping("/used")
    public String getUsed() {
        return this.snowflake.getWorkerId() + "-" + this.snowflake.getDataCenterId();
    }
}
